package com.campus.controller;

import com.campus.pojo.Pager;
import com.campus.pojo.ProParamInfo;
import com.campus.pojo.ProUnitInfo;
import com.campus.service.UnitInfoService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/unitinfo")
public class UnitInfoController {
    @Autowired
    UnitInfoService service;

    @RequestMapping("/queryUnitInfoList_table")//分页查询
    @ResponseBody
    public Map<String,Object> queryUnitInfoList_table(int page, int rows,
                                                      HttpServletResponse response,
                                                      HttpServletRequest request,
                                                      String nameOrCode,
                                                      String unitTypeId,
                                                      String unitGradeId){

        String areaId = (String) request.getSession().getAttribute("AREANUMBER");//获取登录用户的areaNumber
        Pager pager = new Pager();
        pager.setCurPage(page);
        pager.setPerPageRows(rows);
        Map<String ,Object> params = new HashMap<>();
        params.put("pager",pager);
        params.put("areaId",areaId);
        //-----按条件搜索
        params.put("nameOrCode",nameOrCode);
        params.put("unitTypeId",unitTypeId);
        params.put("unitGradeId",unitGradeId);
        //-----
        List<ProUnitInfo> proUnitInfoList = service.selectByPage(params);
        int totalCount = service.count(params);
        Map<String ,Object> result = new HashMap<>();
        result.put("total",totalCount);
        result.put("rows",proUnitInfoList);
        return result;
    }

    @RequestMapping("/getUnitTypeList")
    @ResponseBody
    public Map<String,Object> getUnitTypeList(HttpServletRequest request){    //查找所有类型
        String areaId = (String) request.getSession().getAttribute("AREANUMBER");//获取登录用户的areaNumber
        List<ProParamInfo> unitTypeList = service.selectAllType(areaId);
        Map<String,Object> result = new HashMap<>();
        result.put("unitTypeList",unitTypeList);
        return result;
    }

    @RequestMapping("/addUnit")
    @ResponseBody
    public void addUnit(HttpServletResponse response,HttpServletRequest request,ProUnitInfo unit) throws IOException {  //添加单位
        JsonObject jsonObject = new JsonObject();
        ServletOutputStream out = response.getOutputStream();
        if (service.isRepeat(unit) > 0){
            jsonObject.addProperty("exist","exitOutCode");
            out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
            out.flush();
            out.close();
            return;
        }
        String areaId = (String) request.getSession().getAttribute("AREANUMBER");
        unit.setAreaId(areaId);
        int i = service.addUnitInfo(unit);
        if (i>0){
            jsonObject.addProperty("success",true);
        }else {
            jsonObject.addProperty("success",false);
        }
        out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
        out.flush();
        out.close();
    }

    @RequestMapping("/delUnit")
    @ResponseBody
    public void delUnit(HttpServletResponse response,
                        HttpServletRequest request,
                        String ids) throws IOException{

        JsonObject jsonObject = new JsonObject();
        String areaId = (String)request.getSession().getAttribute("AREANUMBER");
        Map<String,Object> params = new HashMap<>();
        params.put("areaId",areaId);
        params.put("ids",ids);
        int i = service.deleteUnit(params);
        //System.out.println(i);//调试
        if (i > 0){
            jsonObject.addProperty("success",true);
        }
        else {
            jsonObject.addProperty("success",false);
        }
        ServletOutputStream out = response.getOutputStream();
        out.write(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
        out.flush();
        out.close();
    }

    // 获取机构
    @RequestMapping(value = "/getUnitList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> getUnitList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String areaId = req.getSession().getAttribute("AREANUMBER").toString();

        List<ProUnitInfo> list = service.getUnitList(areaId);
        int count = 0;
        if (list != null && list.size() > 0) {
            count = list.size();
        }
        Map<String, Object> result = new HashMap<String, Object>(2);
        result.put("count", count);
        result.put("value", list);
        return result;
    }

    // 查询所有单位信息
    @RequestMapping(value = "/queryUnitInfoList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> queryUnitInfoList(String nameOrCode, String unitTypeId, String unitGradeId,
                                                 HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String areaId = (String) req.getSession().getAttribute("AREANUMBER");
        List<ProUnitInfo> list = service.queryUnitInfoList(nameOrCode, unitTypeId, unitGradeId, areaId);
        int count = 0;
        if (list != null && list.size() > 0) {
            count = list.size();
        }

        Map<String, Object> result = new HashMap<String, Object>(2);
        result.put("count", count);
        result.put("unitInfoList", list);

        return result;
    }

}
